//package com.bestcem.xm.qdes.grpc.service;
//
//import cn.hutool.core.util.StrUtil;
//import com.bestcem.xm.common.grpc.v1.services.BpEmailServiceGrpc;
//import com.bestcem.xm.common.grpc.v1.services.SendEmailNoticeRequest;
//import com.bestcem.xm.common.grpc.v1.services.SendEmailNoticeResponse;
//import io.grpc.stub.StreamObserver;
//import lombok.NonNull;
//import lombok.RequiredArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import net.devh.boot.grpc.server.service.GrpcService;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.mail.SimpleMailMessage;
//import org.springframework.mail.javamail.JavaMailSender;
//import org.springframework.mail.javamail.JavaMailSenderImpl;
//
//import java.io.IOException;
//import java.net.InetSocketAddress;
//import java.net.Socket;
//import java.util.List;
//import java.util.Objects;
//import java.util.Properties;
//
//
///**
// * 发送邮件gRpc接口
// *
// * @author xiaoshu.shao <xiaoshu.shao@idiaoyan.com>
// * @version V1.0
// * @date 2022/8/9
// */
//@GrpcService
//@Slf4j
//@RequiredArgsConstructor
//public class BpEmailGrpcService extends BpEmailServiceGrpc.BpEmailServiceImplBase {
//
//    private @NonNull JavaMailSender javaMailSender;
//
//    @Override
//    public void sendEmailNotice(SendEmailNoticeRequest request, StreamObserver<SendEmailNoticeResponse> responseObserver) {
//        SimpleMailMessage dto = new SimpleMailMessage();
//        List<String> receiveUsersList = request.getReceiveUsersList();
//        //收件人
//        dto.setTo(receiveUsersList.toArray(new String[receiveUsersList.size()]));
//        List<String> ccUsersList = request.getCcUsersList();
//        //抄送人
//        dto.setCc(ccUsersList.toArray(new String[ccUsersList.size()]));
//        //主题
//        dto.setSubject(request.getSubject());
//        //内容
//        dto.setText(request.getContent());
//
//        JavaMailSenderImpl mailSender = null;
//        SendEmailNoticeResponse.Builder builder = SendEmailNoticeResponse.newBuilder();
//        //使用默认邮箱
//        if (StrUtil.isBlank(request.getSmtpId())) {
//            mailSender = (JavaMailSenderImpl) javaMailSender;
//        } else {
//            UserSmtp userSmtp = smtpService.getById(request.getSmtpId());
//            if (Objects.isNull(userSmtp)) {
//                log.error("没有找到对应的[{}]用户邮箱", request.getSmtpId());
//                builder.setStatus(GrpcConstant.FAIL_STATUS);
//                responseObserver.onNext(builder.build());
//                responseObserver.onCompleted();
//                return;
//            } else {
//                mailSender = getMailSender(userSmtp);
//            }
//        }
//
//        try {
//            dto.setFrom(mailSender.getUsername());
//            mailSender.send(dto);
//            builder.setStatus(GrpcConstant.SUCCESS_STATUS);
//        } catch (Exception e) {
//            log.error("邮件发送异常：{}", e);
//            builder.setStatus(GrpcConstant.FAIL_STATUS);
//        }
//        responseObserver.onNext(builder.build());
//        responseObserver.onCompleted();
//    }
//
//
//    public JavaMailSenderImpl getMailSender(UserSmtp userSmtp) {
//        try (Socket socket = new Socket()) {
//            socket.connect(new InetSocketAddress(userSmtp.getHost(), Integer.valueOf(userSmtp.getPort())), 1000);
//        } catch (IOException e) {
//            log.error("邮箱域名:{},端口:{} 无效 ;", userSmtp.getHost(), userSmtp.getPort());
//        }
//
//        JavaMailSenderImpl sender = new JavaMailSenderImpl();
//        //sender.setDefaultEncoding("utf-8");
//        sender.setHost(userSmtp.getHost());
//        if (StringUtils.isNotEmpty(userSmtp.getPort())) {
//            sender.setPort(Integer.valueOf(userSmtp.getPort()));
//        }
//        sender.setUsername(userSmtp.getAccount());
//        sender.setPassword(userSmtp.getPassword());
//        Properties properties = new Properties();
//        // 设置超时时间
//        properties.setProperty("mail.smtp.timeout", "5000");
//        // 如果是ssl的话,需要设置以下属性
//        if (Boolean.TRUE.equals(userSmtp.getIsSsl())) {
//            properties.setProperty("mail.smtp.auth", "true");
//            properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
//            properties.setProperty("mail.smtp.socketFactory.port", userSmtp.getPort());
//        }
//        sender.setJavaMailProperties(properties);
//        return sender;
//    }
//
//    public JavaMailSenderImpl getTestMailSender(UserSmtp userSmtp) {
//        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
//        javaMailSender.setDefaultEncoding("utf-8");
//        javaMailSender.setHost("smtp.qq.com");
//        javaMailSender.setUsername("1905334348@qq.com");
//        javaMailSender.setPassword("hdggmfxoozpsbiee");
//        return javaMailSender;
//    }
//
//    public JavaMailSenderImpl getTestMailSender2(UserSmtp userSmtp) {
//        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
//        javaMailSender.setDefaultEncoding("utf-8");
//        javaMailSender.setHost("smtp.exmail.qq.com");
//        javaMailSender.setUsername("tt.chen@idiaoyan.com");
//        javaMailSender.setPort(465);
//        javaMailSender.setPassword("Ctt201314");
//        return javaMailSender;
//    }
//}
